package com.app.handler;

import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

@RestControllerAdvice
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class GlobelExceptionHandler {

//    public String bindExceptionHandler(Bind) {
//        System.out.println("绑定异常：" + e);
//        return
//    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public String exceptionHandler(MethodArgumentNotValidException e) {
        System.out.println("未知异常：" + e);
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        return fieldErrors.get(0).getDefaultMessage();
    }
}
